/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.Panel;

import utillity.Listeners;
import model.handler.StoreHandler;
import model.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.*;
import javax.swing.PopupFactory;

/**
 *
 * @author MarcoPc
 */
public class UserPanel extends javax.swing.JPanel implements ActionListener {
    
    private JPopupMenu popup;
    private LogIndPanel logInd;
    private StoreHandler storeHandler;
    private Listeners listeners;

    public UserPanel(StoreHandler storeHandler1, Listeners listeners1) {
        setSize(new Dimension(500, 50));
        this.storeHandler = storeHandler1;
        this.listeners = listeners1;
        popup = new JPopupMenu();
        
        listeners.addListener(this);

        initComponents();
        setPicAndName();

        
    }
    
    public void setPicAndName() {
        
        try {
            jLabel1.setText(storeHandler.getEmployee().getName());
            jButton_logInd.setEnabled(false);
            jButton_logUd.setEnabled(true);
        } catch (NullPointerException ex) {
            jLabel1.setText("Bruger ikke logget ind");
            jButton_logInd.setEnabled(true);
            jButton_logUd.setEnabled(false);
        }
        
    }
    
    public void logInd() {
        popUpPanel();
        
    }
    
    public void logUd() {
        storeHandler.logoutEmployee();
        setPicAndName();
    }
    public void closepopup(){
        listeners.notifyListeners("Log In");
//        this.setVisible(false);
    }
    
    public void popUpPanel() {
        listeners.notifyListeners("Log On");
//        logInd.setPreferredSize(new Dimension(220, 400));
//        
//        popup.add(logInd);
//        popup.setLocation(250, 220);
//        popup.setVisible(true);
 
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_logInd = new javax.swing.JButton();
        jButton_logUd = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jButton_logInd.setText("Log Ind");
        jButton_logInd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_logIndActionPerformed(evt);
            }
        });

        jButton_logUd.setText("Log Ud");
        jButton_logUd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_logUdActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel2");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/picter/User.jpg"))); // NOI18N
        jLabel2.setText("jLabel2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(jButton_logUd, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_logInd, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1))
            .addComponent(jButton_logUd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton_logInd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_logUdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_logUdActionPerformed
        logUd();
        closepopup();
    }//GEN-LAST:event_jButton_logUdActionPerformed

    private void jButton_logIndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_logIndActionPerformed
        popUpPanel();
    }//GEN-LAST:event_jButton_logIndActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_logInd;
    private javax.swing.JButton jButton_logUd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent ae) {
        switch (ae.getActionCommand()) {

            
            case "Log":
                setPicAndName();
                break;
            default:

        }
    }
}
